﻿using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ReflexTérkép.Tests.dto;

namespace ReflexTérkép.Tests
{
    [TestClass]
    public class ParentMappingTests :TestBase
    {
        [TestMethod]
        public void Can_Populate_Parent_Objects_Referenced_In_Child_Objects()
        {
            dynamic dynamicCustomer = new ExpandoObject();
            dynamicCustomer.CustomerId = 1;
            dynamicCustomer.FirstName = "Clark";
            dynamicCustomer.LastName = "Kent";
            dynamicCustomer.Addresses_AddressId = 1;
            dynamicCustomer.Addresses_Line1 = "Kent Farm";
            dynamicCustomer.Addresses_Line2 = "Hickory Lane";
            dynamicCustomer.Addresses_City = "Smallville";
            dynamicCustomer.Addresses_State = "Kansas";
            dynamicCustomer.Addresses_ZipCode = "66605";

            dynamic dynamicCustomer2 = new ExpandoObject();
            dynamicCustomer2.CustomerId = 1;
            dynamicCustomer2.FirstName = "Clark";
            dynamicCustomer2.LastName = "Kent";
            dynamicCustomer2.Addresses_AddressId = 2;
            dynamicCustomer2.Addresses_Line1 = "1938 Sullivan Place";
            dynamicCustomer2.Addresses_Line2 = "";
            dynamicCustomer2.Addresses_City = "Metropolis";
            dynamicCustomer2.Addresses_State = "New York ";
            dynamicCustomer2.Addresses_ZipCode = "10012";

            var list = new List<dynamic> { dynamicCustomer, dynamicCustomer2 };


            var customer = ReflexMapper.MapDynamic<ParentMappingCustomer>(list).FirstOrDefault();

            Assert.IsNotNull(customer);
            Assert.IsNotNull(customer.Addresses);
            Assert.IsNotNull(customer.Addresses.FirstOrDefault().Customer);
            Assert.IsTrue(customer.Addresses.FirstOrDefault().Customer == customer);
        }
    }
}
